<?php

/**
 * SmartPHP framework
 *
 * @name SmartPHP framework
 * @package SmartPHP
 * @author 费尔 <admin@smartphp.cn>
 * @version 1.0 Beta
 * @link http://www.smartphp.cn
 * @copyright smartphp.cn
 * @license <a href="http://www.smartphp.cn">smartphp</a>
 */

namespace Base;

/**
 * 调试类
 */
class Debug {

    /**
     * 反向跟踪
     * @return array
     */
    public static function backtrace() {
        $skipfunc[] = 'Base\Debug::backtrace';
        $skipfunc[] = 'Base\Error::system';
        $show = $log = '';
        $debug_backtrace = debug_backtrace();
        krsort($debug_backtrace);
        foreach ($debug_backtrace as $k => $error) {
            $file = str_replace(SMARTPHP_PATH, '', $error['file']);
            $file = str_replace(APP_DIRECTORY, '', $file);
            $file = str_replace(COMMON_PATH, '', $file);
            $func = isset($error['class']) ? $error['class'] : '';
            $func .= isset($error['type']) ? $error['type'] : '';
            $func .= isset($error['function']) ? $error['function'] : '';
            if (in_array($func, $skipfunc)) {
                break;
            }
            $error['line'] = sprintf('%04d', $error['line']);
            $show .= "[Line: {$error['line']}]" . $file . "($func)";
            $show .= "<br/>";
            $log .= !empty($log) ? ' -> ' : "\r\n -> ";
            $file . ':' . $error['line'];
            $log .= $file . ':' . $error['line'];
            $log .= "\r\n";
        }
        return [$show, $log];
    }

    public static function exception($exception) {
        return [$exception->getMessage()];
    }

}
